package com.ztgf.shopping.api.cms.epg;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.epg.EpgSectionBO;
import com.ztgf.shopping.models.bo.epg.EpgSectionSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgSectionDTO;
import com.ztgf.shopping.models.service.epg.IEpgSectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 频道栏目控制器
 *
 * @author YUE
 * @create 2020-05-18 11:36
 */

@RestController
@RequestMapping("/cms/epgSection")
@Slf4j
@Api(value = "EpgSectionController",tags = "频道栏目接口")
public class EpgSectionController {

    @DubboReference
    private IEpgSectionService iEpgSectionService;

    /**
     * 频道栏目列表 根据功能id
     * @param moduleId 功能id
     * @return
     */
    @ApiOperation(value = "频道栏目列表 根据功能id",notes = "频道栏目列表 根据功能id")
    @PostMapping("/getListByModuleId")
    public Result<List<EpgSectionDTO>> getListByModuleId(Long moduleId){
        EpgSectionBO epgSectionBO = new EpgSectionBO();
        epgSectionBO.setModuleId(moduleId);
        List<EpgSectionDTO> epgSectionDTOList = iEpgSectionService.getBySelectSectionInfoNoPage(epgSectionBO);
        return new Result<>(epgSectionDTOList);
    }

    @ApiOperation(value = "添加频道栏目",notes = "添加频道栏目")
    @PostMapping(value = "/insert")
    public Result insert( EpgSectionSaveBO epgSectionSaveBO) {
        if(null == epgSectionSaveBO.getSort())
            return new Result<>(CodeEnum.FAIL_100033);
        boolean flag = iEpgSectionService.add(epgSectionSaveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }


    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<EpgSectionDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        EpgSectionDTO epgSectionDTO = iEpgSectionService.selectById(id);
        return new Result<>(epgSectionDTO);
    }


    @PostMapping("/save")
    @ApiOperation(value = "保存频道栏目",notes = "保存频道栏目")
    public Result save( EpgSectionSaveBO epgSectionSaveBO){
        if(epgSectionSaveBO.getId() == null){
            //添加
            return  insert(epgSectionSaveBO);
        } else {
            return  update(epgSectionSaveBO);
        }
    }

    @ApiOperation(value = "修改频道栏目",notes = "修改频道栏目")
    @PostMapping("/update")
    public Result update( EpgSectionSaveBO epgSectionSaveBO){
        EpgSectionDTO epgSectionDTO = new EpgSectionDTO();
        BeanUtils.copyProperties(epgSectionSaveBO, epgSectionDTO);
        epgSectionDTO.setId(epgSectionSaveBO.getId());
        boolean flag = iEpgSectionService.updateById(epgSectionDTO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除频道栏目",notes = "删除频道栏目")
    @ApiImplicitParam(name = "id",value = "字典表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = iEpgSectionService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除频道栏目",notes = "批量删除频道栏目")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        iEpgSectionService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();

    }

    @ApiOperation(value = "频道栏目列表",notes = "频道栏目列表")
    @PostMapping("/querySelectPageList")
    public Result<Page<EpgSectionDTO>> querySelectPageList( EpgSectionBO epgSectionBO){
        if(null == epgSectionBO)
            return new Result<>(CodeEnum.FAIL_100002 );
        Page<EpgSectionDTO> page = iEpgSectionService.getBySelectSectionInfoPage(epgSectionBO);
        return new Result<>(page);
    }
}
